#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)

hdmimode="720p"

# Parse command line arguments
for arg in $(cat /proc/cmdline); do
  case $arg in
    hdmimode=*)
      hdmimode="${arg#*=}"
      ;;
  esac
done

echo "$hdmimode" > /sys/class/display/mode

# Enable first framebuffer
echo 0 > /sys/class/graphics/fb0/blank

# Disable second framebuffer
echo 1 > /sys/class/graphics/fb1/blank

# Disable framebuffer scaling
echo 0 > /sys/class/graphics/fb0/free_scale

# set initial video state
echo 1 > /sys/class/video/disable_video

# Set default resolution parameters
bpp=32;
xRes=1280;
yRes=720;

# Get max supported display resolution by hdmi device
maxRes=$(tail -n1 /sys/class/amhdmitx/amhdmitx0/disp_cap | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//');
if [ -z "$maxRes" ]; then
  maxRes="$hdmimode";
fi
case "$maxRes" in
  480*)
    xRes=720; yRes=480;
    ;;
  576*)
    xRes=720; yRes=576;
    ;;
  1080*)
    xRes=1920; yRes=1080;
    ;;
  4k2k*hz|2160p*)
    xRes=3840; yRes=2160;
    ;;
  4k2ksmpte|smpte24hz)
    xRes=4096; yRes=2160;
    ;;
esac
vXRes=$xRes;
vYRes=$(( $yRes * 2 ));

# Choose boot resolution by hdmimode param
case "$hdmimode" in
  480*)
    if [ "$vXRes" -ge 720 -a "$vYRes" -ge 960 ]; then
      xRes=720; yRes=480;
    fi
    ;;
  576*)
    if [ "$vXRes" -ge 720 -a "$vYRes" -ge 1152 ]; then
      xRes=720; yRes=576;
    fi
    ;;
  720*)
    if [ "$vXRes" -ge 1280 -a "$vYRes" -ge 1440 ]; then
      xRes=1280; yRes=720;
    fi
    ;;
  1080*)
    if [ "$vXRes" -ge 1920 -a "$vYRes" -ge 2160 ]; then
      xRes=1920; yRes=1080;
    fi
    ;;
  4k2k*hz|2160p*)
    if [ "$vXRes" -ge 3840 -a "$vYRes" -ge 4320 ]; then
      xRes=3840; yRes=2160;
    fi
    ;;
esac

# Set framebuffer geometry to match the resolution
fbset -fb /dev/fb0 -g $xRes $yRes $vXRes $vYRes $bpp;

# Include deinterlacer into default VFM map
echo rm default > /sys/class/vfm/map
echo add default decoder ppmgr deinterlace amvideo > /sys/class/vfm/map
